//
//  STTools_GeXiManger.m
//  WorthWhile
//
//  Created by coder on 2018/10/26.
//  Copyright © 2018年 stoneobs.icloud.com. All rights reserved.
//

#import "STTools_GeXiManger.h"
#import "STTools_GxTiShiAlertView.h"
@interface STTools_GeXiManger()
@property(nonatomic, strong) NSDictionary                     *originDic;/**< 初始字典 */
@end
@implementation STTools_GeXiManger
+ (STTools_GeXiManger *)manger{
    static STTools_GeXiManger * stoneobs_temp_manger;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        stoneobs_temp_manger = STTools_GeXiManger.new;
    });
    return stoneobs_temp_manger;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self stoneobs_fun_sendGxBanBenRequest];
    }
    return self;
}
- (BOOL)stoneobs_fun_should_zidong_panduan{
    return YES;
    if (!self.originDic) {
        return YES;
    }else{
        NSString * stoneobs_temp_ceshi_dengji =  [self.originDic[@"pg_cs_bb"] description];
        NSString * stoneobs_temp_is_zzaishen =  [self.originDic[@"pg_zzsh"] description];
        NSString * stoneobs_temp_dangqian_banben = UIApplication.stoneobs_fun_st_applictionVersin;
        if (stoneobs_temp_ceshi_dengji.floatValue == stoneobs_temp_dangqian_banben.floatValue && stoneobs_temp_is_zzaishen.boolValue) {
            return YES;
        }
        return NO;
    }
    
}
- (BOOL)stoneobs_fun_judgeShould_zhanshi_gx_neirong{
    if (self.originDic) {
        NSString * stoneobs_temp_dangqian_dengji =  [self.originDic[@"pg_zs_bb"] description];
        NSString * stoneobs_temp_dangqian_banben = UIApplication.stoneobs_fun_st_applictionVersin;
        if (stoneobs_temp_dangqian_dengji.floatValue > stoneobs_temp_dangqian_banben.floatValue) {
            //需要更新
            NSString * stoneobs_temp_gx_wenzi = [self.originDic[@"pg_gxnr"] description];
            UIViewController * stoneobs_temp_vc  = TMUtils.stoneobs_fun_get_currentTopController;
            UIWindow * stoneobs_temp_window = UIApplication.sharedApplication.delegate.window;
            
            stoneobs_temp_gx_wenzi =  [stoneobs_temp_gx_wenzi stringByReplacingOccurrencesOfString:@";" withString:@"\n"];
            NSString * stoneobs_temp_gx_dizhi = [self.originDic[@"pg_gxdz"] description];
            NSString *stoneobs_temp_sf_qiangGX = [self.originDic[@"pg_qzgx"] description];
            BOOL stoneobs_temp_qzgx = NO;
            if ([stoneobs_temp_sf_qiangGX isEqualToString:@"stonobesxuyaochuanmen"]) {
                stoneobs_temp_qzgx = YES;
            }
            [STTools_GxTiShiAlertView stoneobs_fun_showGxNeiRongViewWithContent:stoneobs_temp_gx_wenzi isqzgx:stoneobs_temp_qzgx gxlianjieUrl:stoneobs_temp_gx_dizhi];
            
            
        }else{
             return NO;

        }
    }else{
        [self stoneobs_fun_sendGxBanBenRequest];
    }
    return NO;
}
#pragma mark --NetWork Method
- (void)stoneobs_fun_sendGxBanBenRequest{
    NSMutableDictionary * stoneobs_temp_parameters = [NSMutableDictionary new];
    NSString * stoneobs_temp_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"index/mt"];
    [stoneobs_temp_parameters setObject:@"ios" forKey:@"user_from"];
    NSString * stoneobs_temp_version = [NSString stringWithFormat:@"%@",UIApplication.stoneobs_fun_st_applictionVersin];
    [stoneobs_temp_parameters setObject:stoneobs_temp_version forKey:@"version"];
    [stoneobs_temp_parameters setObject:@"apple" forKey:@"register_name"];
    
    DDLogInfo(@"url:%@",stoneobs_temp_url);
    DDLogInfo(@"param:%@",stoneobs_temp_parameters);
    AFHTTPSessionManager * manger = [AFHTTPSessionManager manager];
    manger.requestSerializer = [AFHTTPRequestSerializer serializer];
    manger.requestSerializer.timeoutInterval = 20;
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    
    [manger GET:stoneobs_temp_url parameters:stoneobs_temp_parameters headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary * stoneobs_temp_dataDic = responseObject[@"data"];
        NSString * stoneobs_temp_code = [responseObject[@"code"] description];
        if (stoneobs_temp_code.integerValue == 1) {
            self.originDic = stoneobs_temp_dataDic;
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self stoneobs_fun_judgeShould_zhanshi_gx_neirong];
        });
    }];
    
}
@end

